package com.itservice.uzalarm;

import java.util.Calendar;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.view.SurfaceHolder;

//グラフィックス
public class MazeGraphics 
{
    private SurfaceHolder mHolder	= null;
    private Paint         mPaint	= null;
    private Canvas        mCanvas	= null;

    //グラフィックス
    public MazeGraphics(SurfaceHolder holder)
    {
        mHolder=holder;
        mPaint=new Paint();
        mPaint.setAntiAlias(true);
    }
    
    //ロック
    public void lock()
    {
        mCanvas = mHolder.lockCanvas();
    }
    
    //アンロック
    public void unlock()
    {
    	if(null != mHolder && null != mCanvas)
    	{
    		mHolder.unlockCanvasAndPost(mCanvas);
    	}
    }
    
    //色の指定
    public void setColor(int color)
    {
    	if(null != mPaint)
    	{
    		mPaint.setColor(color);
    	}
    }

    //フォントサイズの指定
    public void setFontSize(int fontSize)
    {
    	if(null != mPaint)
    	{
    		mPaint.setTextSize(fontSize);
    	}
    }
    
    //ビットマップの描画
    public void drawBitmap(Bitmap bitmap,int x,int y)
    {
        int w=bitmap.getWidth();
        int h=bitmap.getHeight();
        Rect src=new Rect(0,0,w,h);
        Rect dst=new Rect(x,y,x+w,y+h);
    	if(null != mCanvas)
    	{
    		mCanvas.drawBitmap(bitmap,src,dst,mPaint);
    	}
    }

    //ビットマップの描画
    public void setCanvasColor(int color)
    {
    	if(null != mCanvas)
    	{
    		mCanvas.drawColor(color);
    	}
    }
    
    //文字列の描画
    public void drawString(String string,int x,int y, int size)
    {
    	if(null == mPaint || null == mCanvas)
    	{
    		return;
    	}
    	mPaint.setTextSize(size);
        mCanvas.drawText(string,x,y,mPaint);
    }

    //画面の保存
    public void saveDisplay()
    {
    	if(null != mCanvas)
    	{
    		mCanvas.save();
    	}
    }

    //画面の復元
    public void restoreDisplay()
    {
    	if(null != mCanvas)
    	{
    		mCanvas.restore();
    	}
    }

    // 時刻の取得
    public void DrawTime()
    {
    	Calendar calendar = Calendar.getInstance();
    	int hour;
    	int min;
    	int sec;
    	String strHour = null;
    	String strMin  = null;
    	String strSec  = null;

    	if(null == mCanvas || null == mPaint)
    	{
    		return;
    	}

    	hour = calendar.get(Calendar.HOUR_OF_DAY);
    	min = calendar.get(Calendar.MINUTE);
    	sec = calendar.get(Calendar.SECOND);

    	strHour = String.valueOf(hour);
    	strMin = String.valueOf(min);
    	strSec = String.valueOf(sec);

    	mPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC));
    	mPaint.setTextSize(50);

    	// 時間の表示
    	if(hour >= 10)
    	{
    		mCanvas.drawText(strHour,40,100,mPaint);
    	}
    	else
    	{
    		mCanvas.drawText("0" + strHour,40,100,mPaint);
    	}
    	mCanvas.drawText(":",100,100,mPaint);

    	// 分の表示
    	if(min >= 10)
    	{
    		mCanvas.drawText(strMin ,120,100,mPaint);
    	}
    	else
    	{
    		mCanvas.drawText("0" + strMin ,120,100,mPaint);
    	}
    	mCanvas.drawText(":",180,100,mPaint);

    	// 秒の表示
    	if(sec >= 10)
    	{
        	mCanvas.drawText(strSec ,210,100,mPaint);
    	}
    	else
    	{
        	mCanvas.drawText("0" + strSec ,210,100,mPaint);
    		
    	}
    }
}
